/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
Cu.import("resource://app/jsmodules/sbProperties.jsm");

// check that gMM exists
if (typeof gMM == "undefined") {
  var gMM = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"].getService(Ci.sbIMediacoreManager);
}

MusicRecommendations = {
  onLoad: function() {
    gMM.addListener(this);
    this._service = Cc["@necimal.com/recommendations;1"].getService().wrappedJSObject;
    this._deck = document.getElementById("music-recommendations-deck");
    
    // do we have a currently playing song?
    if ((gMM.status.state == Ci.sbIMediacoreStatus.STATUS_PLAYING) ||
        (gMM.status.state == Ci.sbIMediacoreStatus.STATUS_PAUSED) ||
        (gMM.status.state == Ci.sbIMediacoreStatus.STATUS_BUFFERING)) {
      var artistName = gMM.sequencer.currentItem.getProperty(SBProperties.artistName);
      this.showArtistInfo(artistName);
    }
  },
  
  onUnload: function() {
    gMM.removeListener(this);
  },
  
  // show tracks for an artist
  showMedia: function(artistName) {
    // create a new view from the main library
    var view = LibraryUtils.mainLibrary.createView();
    
    // create a search by artist name
    var filterIndex = view.cascadeFilterSet.appendSearch([SBProperties.artistName], 1);
    view.cascadeFilterSet.set(filterIndex, [artistName], 1);
    
    // then load the view in the tabbrowser
    window.top.gBrowser.loadMediaList(LibraryUtils.mainLibrary, null, null, view, null);
  },
  
  onMediacoreEvent: function(ev) {
    switch (ev.type) {
      case Ci.sbIMediacoreEvent.TRACK_CHANGE:
        var artistName = ev.data.getProperty(SBProperties.artistName);
        this.showArtistInfo(artistName);
        break;
      case Ci.sbIMediacoreEvent.STREAM_END:
      case Ci.sbIMediacoreEvent.STREAM_STOP:
        this.hideArtistInfo();
        break;
    }
  },
  
  showArtistInfo: function(artistName) {
    if (artistName) {
      // get the new artist info page
      var newArtistDOM = this._service.getArtistDOM(artistName);
      if (!newArtistDOM) { this.hideArtistInfo(); }
      
      // replace the old artist info page
      var oldArtistDOM = document.getElementById("music-recommendations-list");
      this._deck.replaceChild(newArtistDOM, oldArtistDOM);
      this._deck.selectedIndex = 1;
    }
  },
  
  hideArtistInfo: function() {
    // show the splash screen
    document.getElementById("music-recommendations-deck").selectedIndex = 0;
  }
};

// event wireup
window.addEventListener("load", function(e) { MusicRecommendations.onLoad(e); }, false);
window.addEventListener("unload", function(e) { MusicRecommendations.onUnload(e); }, false);
